package org.yaukie.core.impl;

import java.util.HashMap;
import java.util.Map;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaukie.core.anotation.MyJob;
import org.yaukie.core.anotation.MyJob.JobType;

public abstract class MyAbstractJob {
    private static final Logger log = LoggerFactory.getLogger(MyAbstractJob.class);
    public static final Map<Class, Scheduler> schedulerMap = new HashMap();

    public MyAbstractJob() {
    }

    protected void doStartJob(Class jobClass, JobDetail jobDetail, Trigger trigger) {
        try {
            Scheduler scheduler = this.createScheduler(jobDetail, trigger);
            scheduler.start();
            schedulerMap.put(jobClass, scheduler);
            log.debug("[MyJobInstance] started job: " + jobClass.getName());
        } catch (SchedulerException var6) {
            log.error("start job " + jobClass.getSimpleName() + "  error ");
        }

    }

    protected boolean getJobStatus(Class jobClass) {
        boolean flag = false;
        if (schedulerMap.containsKey(jobClass)) {
            Scheduler scheduler = (Scheduler)schedulerMap.get(jobClass);

            try {
                flag = scheduler.isStarted();
                if (flag && log.isDebugEnabled()) {
                    log.debug("定时器" + jobClass.getSimpleName() + "已经启动,无需重复启动!");
                }
            } catch (SchedulerException var5) {
                if (log.isErrorEnabled()) {
                    log.error("定时器获取启动状态异常" + var5.getMessage());
                }
            }
        }

        return flag;
    }

    protected SimpleTrigger createJobSimpleTriger(Class jobClass, int count, int seconds, String start, String end) {
        if (jobClass.isAnnotationPresent(MyJob.class)) {
            MyJob job = (MyJob)jobClass.getAnnotation(MyJob.class);
            MyJob.JobType jobType = job.jobType();
            if (jobType.equals(JobType.TIMER)) {
                count = job.count();
                seconds = job.seconds();
                start = job.start();
                end = job.end();
            }
        }

        TriggerBuilder triggerBuilder;
        if (count > 0) {
            triggerBuilder = TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.repeatSecondlyForTotalCount(count, seconds));
        } else {
            triggerBuilder = TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(seconds));
        }

        return this.doCreateJobSimpleTrigger(triggerBuilder, start, end);
    }

    protected Trigger createJobTrigger(Class jobClass, String defaultCron) {
        String cron = defaultCron;
        if (jobClass.isAnnotationPresent(MyJob.class)) {
            MyJob job = (MyJob)jobClass.getAnnotation(MyJob.class);
            MyJob.JobType type = job.jobType();
            if (type.equals(JobType.CRON)) {
                cron = job.value();
            }
        }

        return TriggerBuilder.newTrigger().withIdentity(jobClass.getName()).withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
    }

    abstract Scheduler createScheduler(JobDetail var1, Trigger var2) throws SchedulerException;

    abstract Scheduler getScheduler(Class var1);

    abstract SimpleTrigger doCreateJobSimpleTrigger(TriggerBuilder<SimpleTrigger> var1, String var2, String var3);
}
